package com.zut.controller;

import com.zut.domain.Admin;
import com.zut.service.AdminService;
import com.zut.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /*管理员登录*/
    @PostMapping(value = "/login")
    public Result getUserLogin(@RequestBody Admin admin) {
        Admin admin1 = adminService.getByNP(admin);
        if (admin1 != null) {
            String token = JwtUtil.sign(admin.getId(), admin.getPassword());
            HashMap users = new HashMap();
            users.put("token", token);
            users.put("id", admin1.getId());
            users.put("name", admin1.getName());
            return new Result(Code.GET_OK, users, "登录成功！");
        } else {
            return new Result(Code.GET_ERR, null, "用户名或密码错误！");
        }
    }

    /*修改管理员信息*/
    @PutMapping
    public Result Update(@RequestBody Admin admin) {
        boolean flag = adminService.update(admin);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }



}
